<template>
    <input :type="ipttype" class="c-input" maxlength="40" v-model="inputed" @input="e_input">
</template>

<script>
export default {
	name: 'c-input',
	props: {
		onlyNumber: {
			default: true,
		},
		ipttype: {
			default: 'text',
		},
	},
	data() {
		return {
			inputed: '',
		}
	},
	created() {},
	methods: {
		e_input() {
			if (this.onlyNumber) {
				this.handle()
			}
			this.$emit('input', this.inputed)
		},
		handle() {
			let v = this.inputed
			let re = ''
			let len = v.length
			let pointCount = 0
			for (let i = 0; i < len; i++) {
				let a = v[i]
				a = a.replace(/[^.\d]/, '')
				// 去掉多余点号
				if (a.match(/[.]/)) {
					pointCount++
					if (pointCount > 1) {
						a = ''
					}
				}
				re += a
			}
			// 小数点开头就添加0
			re = re.replace(/^[.]/, '0.')
			// 去掉开头过多的0
			re = re.replace(/^[0]+/, '0')
			this.inputed = re
		},
	},
}
</script>

<style scoped>
.c-input {
	width: 300px;
	border-radius: 4px;
	padding-left: 10px;
	line-height: 20px;
	font-size: 14px;
	background-color: rgba(255, 255, 255, 0.219);
}
</style>